from django.shortcuts import HttpResponse
from django.shortcuts import render

import requests


# 视图
def hello(request):
    return HttpResponse("hello ~")


def hello1(request, id):
    print(id)
    return HttpResponse("hello1 ~")


list1 = []

import os

is_first = 1


def son(request):
    return render(request, "son.html")


def name(request):
    return render(request, "index.html", context={"name": "张三"})


def index(request):
    # META  data 元数据   描述数据的数据
    print(request)
    # print(dir(request))
    # print(request.META)
    # print(type(request.META))
    # sex = request.GET.get("sex")
    # list1 存放ip
    print(request.META.get("HTTP_X_FORWARDED_FOR"))

    name = request.GET.get("name")

    # user_ip = request.META['REMOTE_ADDR']
    if 'HTTP_X_FORWARDED_FOR' in request.META:  # 获取用户真实IP地址
        user_ip = request.META['HTTP_X_FORWARDED_FOR']
    else:
        user_ip = request.META['REMOTE_ADDR']
    # if 没有ip.txt文件
    global is_first
    global list1
    if not os.path.exists("ip.txt"):
        if user_ip not in list1:
            save_file("ip.txt", "{} {}".format(user_ip, name))
            list1.append(user_ip)
    else:
        if is_first:
            list1 = readfile('ip.txt')
            is_first = 0
        else:
            if user_ip not in list1:
                save_file("ip.txt", "{} {}".format(user_ip, name))
                list1.append(user_ip)

    # if is_first:
    #     is_first = 0
    #     if not os.path.exists("ip.txt"):
    #         #   创建文件写入 ip
    #         if user_ip not in list1:
    #             save_file("ip.txt", "{} {}".format(user_ip, name))
    #             print(list1)
    #             list1.append(user_ip)
    #     else:  # if 有这个ip.txt文件
    #         # 从ip.txt文件 中 读出ip  存放到list1中
    #         list1.extend(readfile('ip.txt'))
    #         print("第二次", list1)
    # else:  # 不是第一次访问
    #     if user_ip not in list1:
    #         save_file("ip.txt", "{} {}".format(user_ip, name))
    #         list1.append(user_ip)
    #         print("不是第二次", list1)

    # if not os.path.exists("ip.txt"):
    #         #   创建文件写入 ip
    #     if user_ip not in list1:
    #         save_file("ip.txt", "{} {}".format(user_ip, name))
    #         list1.append(user_ip)
    # else:  # if 有这个ip.txt文件
    #         # 从ip.txt文件 中 读出ip  存放到list1中
    #     if user_ip not in list1:
    #         save_file("ip.txt", "{} {}".format(user_ip, name))
    #         list1.append(user_ip)
    #     list1.extend(readfile('ip.txt'))

    return render(request, "index.html")


def readfile(filename):
    with open(filename, mode="r", encoding="utf-8") as f:
        #     [x表达式 for x变量 in 一个范围]
        list2 = [x.split(" ")[0] for x in f.readlines()]
    return list2


def save_file(filename, text):
    """
    将一段文本保存在一个文件中
    :param filename: 保存在哪个文件中
    :param text: 文本内容
    :return:
    """
    with open(filename, mode="a+", encoding="utf-8") as f:
        f.write(text)
        f.write("\n")
